﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base;
using UFIDA.U9.Base.UserRole;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFSoft.UBF.Business;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.Cust.YYD.PlugBE.SupplySourceP
{
    public class AfterUpdating : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.CBO.SCM.Supplier.SupplySource;
            if (holder == null)
                return;
            #endregion

            //提交传
            if (holder.OriginalData.Status == CBO.SCM.Supplier.SupplierSourceStatusEnum.OpenStatus && holder.Status == CBO.SCM.Supplier.SupplierSourceStatusEnum.ApprovingStatus)
            {
                //新增
                if (string.IsNullOrEmpty(holder.DescFlexField.PrivateDescSeg1))
                {
                    HttpHelper httpHelper = new HttpHelper();
                    var token = httpHelper.GetOAToken();
                    JsonConfig config = new JsonConfig();
                    httpHelper.URL = config.SelectToken("OA.URL") + "/papi/openapi/api/workflow/core/paService/v1/doCreateRequest";

                    User user = User.Finder.FindByID(Context.LoginUserID);
                    var userinfo = httpHelper.GetOAUserInfo(token, user.Code);

                    OADTO dto = new OADTO();
                    dto.access_token = token;
                    dto.userid = userinfo.Key;
                    dto.workflowId = "1201798885423398989";
                    var formData = new FormData();
                    formData.formId = "1201798885423398912";
                    var dataDetails = new List<DataDetailsItem>();



                    //申请人
                    DataDetailsItem d1 = new DataDetailsItem();
                    d1.dataOptions = new List<DataOptionsItem> {new DataOptionsItem{
                        content = Context.LoginUser,
                        optionId = userinfo.Key,
                        type ="resource"
                    }};
                    d1.dataKey = "sqr";
                    dataDetails.Add(d1);

                    //申请部门
                    DataDetailsItem d2 = new DataDetailsItem();
                    d2.dataOptions = new List<DataOptionsItem> {new DataOptionsItem{
                        content="",
                        optionId= userinfo.Value,
                        type="department"
                    }};
                    d2.dataKey = "sqbm";
                    dataDetails.Add(d2);

                    //申请日期
                    DataDetailsItem d3 = new DataDetailsItem();
                    d3.content = DateTime.Now.Date.ToShortDateString();
                    d3.dataKey = "sqrq";
                    dataDetails.Add(d3);

                    //供应商名称
                    DataDetailsItem d4 = new DataDetailsItem();
                    d4.content = holder.SupplierInfo.Supplier.Name;
                    d4.dataKey = "gysmc";
                    dataDetails.Add(d4);

                    //料号
                    DataDetailsItem d5 = new DataDetailsItem();
                    d5.content = holder.ItemInfo.ItemID.Code;
                    d5.dataKey = "lh";
                    dataDetails.Add(d5);

                    //品名
                    DataDetailsItem d6 = new DataDetailsItem();
                    d6.content = holder.ItemInfo.ItemID.Name;
                    d6.dataKey = "pm";
                    dataDetails.Add(d6);

                    //配额方式
                    string sql1 = $@" select dbo.F_GetEnumName('UFIDA.U9.CBO.SCM.Item.PurchaseQuotaModeEnum','{holder.ItemInfo.ItemID.PurchaseInfo.PurchaseQuotaMode.Value}','zh-CN') PurchaseQuotaMode";
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out DataSet ds1);
                    string PurchaseQuotaMode = ds1.Tables[0].Rows[0]["PurchaseQuotaMode"].ToString();
                    DataDetailsItem d7 = new DataDetailsItem();
                    d7.content = PurchaseQuotaMode;
                    d7.dataKey = "pefs";
                    dataDetails.Add(d7);


                    //供货顺序
                    DataDetailsItem d8 = new DataDetailsItem();
                    d8.content = holder.OrderNO.ToString();
                    d8.dataKey = "ghsx";
                    dataDetails.Add(d8);

                    //配额比例
                    DataDetailsItem d9 = new DataDetailsItem();
                    d9.content = holder.SupplierQuota.ToString("P2");
                    d9.dataKey = "pebl";
                    dataDetails.Add(d9);

                    //采购备量
                    DataDetailsItem d10 = new DataDetailsItem();
                    d10.content = holder.PurchaseBatchQty.ToString();
                    d10.dataKey = "cgbl";
                    dataDetails.Add(d10);

                    //生效日期
                    DataDetailsItem d11 = new DataDetailsItem();
                    d11.content = holder.Effective.EffectiveDate.ToShortDateString();
                    d11.dataKey = "sxrq1";
                    dataDetails.Add(d11);

                    //失效日期
                    DataDetailsItem d12 = new DataDetailsItem();
                    d12.content = holder.Effective.DisableDate.ToShortDateString();
                    d12.dataKey = "sxrq";
                    dataDetails.Add(d12);
            

                    formData.dataDetails = dataDetails;
                    dto.formData = formData;
                    dto.otherParams = new OtherParams { isnextflow = "1" };
                    httpHelper.Content = JsonConvert.SerializeObject(dto);
                    var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                    if (result["message"]["errmsg"].ToString() != "success")
                    {
                        throw new Exception(result["message"]["errmsg"].ToString());
                    }

                    holder.DescFlexField.PrivateDescSeg1 = result["message"]["requestId"].ToString();
                }
                else
                {
                    HttpHelper httpHelper = new HttpHelper();
                    var token = httpHelper.GetOAToken();
                    JsonConfig config = new JsonConfig();
                    httpHelper.URL = config.SelectToken("OA.URL") + "/papi/openapi/api/workflow/core/paService/v1/submitRequest";

                    User user = User.Finder.FindByID(Context.LoginUserID);
                    var userinfo = httpHelper.GetOAUserInfo(token, user.Code);

                    OAUpdateDTO dto = new OAUpdateDTO();
                    dto.access_token = token;
                    dto.userid = userinfo.Key;
                    dto.requestId = holder.DescFlexField.PrivateDescSeg1;
                    var formData = new FormData();
                    formData.formId = "1201798885423398912";
                    var dataDetails = new List<DataDetailsItem>();



                    //申请人
                    DataDetailsItem d1 = new DataDetailsItem();
                    d1.dataOptions = new List<DataOptionsItem> {new DataOptionsItem{
                        content = Context.LoginUser,
                        optionId = userinfo.Key,
                        type ="resource"
                    }};
                    d1.dataKey = "sqr";
                    dataDetails.Add(d1);

                    //申请部门
                    DataDetailsItem d2 = new DataDetailsItem();
                    d2.dataOptions = new List<DataOptionsItem> {new DataOptionsItem{
                        content="",
                        optionId= userinfo.Value,
                        type="department"
                    }};
                    d2.dataKey = "sqbm";
                    dataDetails.Add(d2);

                    //申请日期
                    DataDetailsItem d3 = new DataDetailsItem();
                    d3.content = DateTime.Now.Date.ToShortDateString();
                    d3.dataKey = "sqrq";
                    dataDetails.Add(d3);

                    //供应商名称
                    DataDetailsItem d4 = new DataDetailsItem();
                    d4.content = holder.SupplierInfo.Supplier.Name;
                    d4.dataKey = "gysmc";
                    dataDetails.Add(d4);

                    //料号
                    DataDetailsItem d5 = new DataDetailsItem();
                    d5.content = holder.ItemInfo.ItemID.Code;
                    d5.dataKey = "lh";
                    dataDetails.Add(d5);

                    //品名
                    DataDetailsItem d6 = new DataDetailsItem();
                    d6.content = holder.ItemInfo.ItemID.Name;
                    d6.dataKey = "pm";
                    dataDetails.Add(d6);

                    //配额方式
                    string sql1 = $@" select dbo.F_GetEnumName('UFIDA.U9.CBO.SCM.Item.PurchaseQuotaModeEnum','{holder.ItemInfo.ItemID.PurchaseInfo.PurchaseQuotaMode.Value}','zh-CN') PurchaseQuotaMode";
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out DataSet ds1);
                    string PurchaseQuotaMode = ds1.Tables[0].Rows[0]["PurchaseQuotaMode"].ToString();
                    DataDetailsItem d7 = new DataDetailsItem();
                    d7.content = PurchaseQuotaMode;
                    d7.dataKey = "pefs";
                    dataDetails.Add(d7);


                    //供货顺序
                    DataDetailsItem d8 = new DataDetailsItem();
                    d8.content = holder.OrderNO.ToString();
                    d8.dataKey = "ghsx";
                    dataDetails.Add(d8);

                    //配额比例
                    DataDetailsItem d9 = new DataDetailsItem();
                    d9.content = holder.SupplierQuota.ToString("P2");
                    d9.dataKey = "pebl";
                    dataDetails.Add(d9);

                    //采购备量
                    DataDetailsItem d10 = new DataDetailsItem();
                    d10.content = holder.PurchaseBatchQty.ToString();
                    d10.dataKey = "cgbl";
                    dataDetails.Add(d10);

                    //生效日期
                    DataDetailsItem d11 = new DataDetailsItem();
                    d11.content = holder.Effective.EffectiveDate.ToShortDateString();
                    d11.dataKey = "sxrq1";
                    dataDetails.Add(d11);

                    //失效日期
                    DataDetailsItem d12 = new DataDetailsItem();
                    d12.content = holder.Effective.DisableDate.ToShortDateString();
                    d12.dataKey = "sxrq";
                    dataDetails.Add(d12);


                    formData.dataDetails = dataDetails;
                    dto.formData = formData;
                    var Content = JsonConvert.SerializeObject(dto);
                    httpHelper.Content = Content;
                    var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                    if (result["message"]["errmsg"].ToString() != "success")
                    {
                        throw new Exception(result["message"]["errmsg"].ToString());
                    }
                }
              
            }
        }
    }
}
